package ftest.movie.appLogic2;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

import ftest.movie.pages.PageManager;


public class DriverBasedHelper {

  protected WebDriver driver;
  protected WebDriverWait wait;
  protected PageManager pages;
  
  public int Timeout = 30;

  public DriverBasedHelper(WebDriver driver) {
    this.driver = driver;
    wait = new WebDriverWait(driver, 10);
    pages = new PageManager(driver);
  }
  
  public void clickEnterButton(WebElement element){
	  element.click();
	  element.sendKeys(Keys.ENTER);
  }
  
  public void input(WebElement element, String sendText)
  {
      if (sendText == null)
      {
          return;
      }
      element.clear();
      element.sendKeys(sendText);
  }
  
  public boolean isElementPresent(By element)
  {
	  boolean isPresent = true;
      try
      {
          setBrowserTimeoutInMilisec(10);
          driver.findElement(element);
      }
      catch (Exception e)
      {
          setBrowserTimeoutInSec(Timeout);
          isPresent = false;
      }
      return isPresent;
  }
  
  public boolean isElementPresent(WebElement element)
  {
	  boolean isPresent;
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      try
      {
          Point location = element.getLocation();
          isPresent = true;
      }
      catch (Exception e)
      {
          isPresent = false;
      }
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      return isPresent;
  }

  public boolean isElementDisplayed(By element)
  {
      try
      {
          return isElementPresent(element) && driver.findElement(element).isDisplayed();
      }
      catch (Exception e)
      {
      }
      return false;
  }
  
  public boolean isElementDisplayed(WebElement element)
  {
      try
      {
          return isElementPresent(element) && element.isDisplayed();
      }
      catch (Exception e)
      {
          return false;
      }
  }
  
  public void setBrowserTimeoutInMilisec(int timeoutInMillisec)
  {
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }
  
  public void setBrowserTimeoutInSec(int timeoutInSec)
  {
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }
  
	public void alertOk() {
		Alert alert = driver.switchTo().alert();
		String alertTxt = alert.getText();
		alert.accept();
	}

	}
